/**
 * 时间 ： 2014-6-9 下午2:12:34
 * 作者 ： HaiTao
 * 描述 ： 当前类的描述信息
 **/
package com.laitaoo.service
{
	import com.laitaoo.event.StorageEvent;
	import com.maclema.mysql.MySqlResponse;
	import com.maclema.mysql.MySqlToken;
	import com.maclema.mysql.ResultSet;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.AsyncResponder;

	[RouteEvents(events="storageListResult,addStorageResult")]
	[Component]
	public class StorageService extends BaseService
	{
		public function StorageService()
		{
			super();
		}
		/**
		 * 所有种类的集合
		 **/
		private var kindRs:ArrayCollection;
		
		/**
		 * 获取所有的库存货物 ,在获取所有库存之前需要获取所有的分类
		 **/
		public function storageList():void{
			
			getKindList(resultHandler,faultHandler);
		}
		
		private function resultHandler(data:Object, token:Object):void{
			
			kindRs = ResultSet(data).getRows();
			
			var sql:String = "select * from product LEFT JOIN storage ON product.pId=storage.productId";
			
			var tokens:MySqlToken = st.executeQuery(sql);
			
			tokens.addResponder(new AsyncResponder(storageResultHandler,storageFaultHandler,tokens));
			
		}
		private function faultHandler(info:Object, token:Object):void{
			//获取分类失败
		}
		
		private function storageResultHandler(data:Object, token:Object):void{
			
			var rs:ResultSet = ResultSet(data);
			
			var rows:ArrayCollection = rs.getRows();
			
			var totalCost:Number = 0; // 成本总价
			
			for each (var storageObj:Object in rows) 
			{
				for each (var kindObj:Object in kindRs) 
				{
					if(kindObj.children == null)// 初始化数据
						kindObj.children = [];
					
					if(kindObj.Id == storageObj.kindId){
						(kindObj.children as Array).push(storageObj);
					}
				}
				totalCost = totalCost + Number(storageObj.totalPrice);
			}
			
			var storageEvent:StorageEvent = new StorageEvent(StorageEvent.storageListResult);
			storageEvent.storageList = kindRs;
			storageEvent.storageTotalCost = totalCost+"";
			dispatchEvent(storageEvent);
		}
		private function storageFaultHandler(info:Object, token:Object):void{
			//获取分类失败
		}
		
		// ------------------------添加货物 ----------------------------
		
		/**
		 * 添加货物
		 * */
		public function addStorage(price:String,count:String,totalPrice:String,productId:String):void{
			
			var sql:String = "insert into storage(productId,count,price,totalPrice) values('"+productId+"','"+count+"','"+price+"','"+totalPrice+"')";
			
			var tokens:MySqlToken = st.executeQuery(sql);
			
			tokens.addResponder(new AsyncResponder(addStorageResultHandler,addStorageFaultHandler,tokens));
		}
		private function addStorageResultHandler(data:Object, token:Object):void{
			
			var rs:MySqlResponse = MySqlResponse(data);
			
			var storageEvent:StorageEvent = new StorageEvent(StorageEvent.addStorageResult);
			dispatchEvent(storageEvent);
		}
		private function addStorageFaultHandler(info:Object, token:Object):void{
			//获取分类失败
		}
		
		/**
		 * 编辑货物
		 * */
		public function updateStorage(price:String,count:String,totalPrice:String,storageId:String):void{
			
			var sql:String = "update storage set price='"+price+"',count='"+count+"',totalPrice='"+totalPrice+"' where storageId='"+storageId+"'";
			
			var tokens:MySqlToken = st.executeQuery(sql);
			
			tokens.addResponder(new AsyncResponder(updateStorageResultHandler,updateStorageFaultHandler,tokens));
		}
		private function updateStorageResultHandler(data:Object, token:Object):void{
			
			var rs:MySqlResponse = MySqlResponse(data);
			
			var storageEvent:StorageEvent = new StorageEvent(StorageEvent.addStorageResult);
			dispatchEvent(storageEvent);
		}
		private function updateStorageFaultHandler(info:Object, token:Object):void{
			//获取分类失败
		}
	}
}





